﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using KPIS.GERP.GBMS.MODEL;
using System.Collections;


namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Approve_User : System.Web.UI.Page
    {
        private ComboBLL comboBLL = new ComboBLL();
        private IProject projectBLL;

        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.QueryString["type"].ToString();

            if (type.Length > 0)
            {
                hdType.Value = type;
                projectBLL = Project_Factory.GetProjectFactory(type);
            }

            if (!Page.IsPostBack)
            {
                LoadYear();
                LoadDDLJobsiteSeq();
                ddlYear_SelectedIndexChanged(null, null);
            }
        }

        private void LoadYear()
        {
            int CurrYear = SystemInfo._CurrentYearReq;
            ddlYear.Items.Clear();

            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                ddlYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }

            ddlYear.SelectedValue = CurrYear.ToString();
        }

        private void LoadDDLJobsiteSeq()
        {
            DataTable dt = comboBLL.ListJobsite();
            ddlJobsiteSeq.DataSource = dt;
            ddlJobsiteSeq.DataBind();

            ddlJobsiteSeq.Items.Insert(0, new RadComboBoxItem("----- เลือกหน่วยงาน -----", ""));
        }

        private void ClearGrid()
        {
            gridSource.DataSource = null;
            gridSource.DataBind();
        }

        protected void ddlYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLStrategySeq();
        }

        protected void ddlJobsiteSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLStrategySeq();
        }

        protected void LoadDDLStrategySeq()
        {
            ddlStrategy.DataSource = comboBLL.ListStrategy(ddlYear.SelectedValue, "");
            ddlStrategy.DataBind();
        }
        protected void ddlStrategy_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if(ddlStrategy.SelectedValue.Length > 0)
                LoadData();
        }

        private void LoadData()
        {
            LoadDataSource();
        }
        private void LoadDataSource()
        {
            //--ระดับกิจกรรม   column = แหล่งเงิน | ขอ | อนุมัติ  GetDataProjectSourceByStrategySequence
            gridSource.DataSource = projectBLL.GetDataProjectAndSource("", ddlStrategy.SelectedValue);
            gridSource.DataBind();
        }

        protected void gridSource_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                //Control target = e.Item.FindControl("hlChange");
                //if (!Object.Equals(target, null))
                //{
                //    if (!Object.Equals(this.RadToolTipManager1, null))
                //    {
                //        //Add the button (target) id to the tooltip manager
                //        this.RadToolTipManager1.TargetControls.Add(target.ClientID, "S-" + (e.Item as GridDataItem).GetDataKeyValue("Sequence").ToString(), true);
                //    }
                //}
            }
        }
        //protected void gridProject_ItemDataBound(object sender, GridItemEventArgs e)
        //{
        //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        //    {
        //        var dataItem = e.Item as GridDataItem;
        //        if (Convert.ToDouble(dataItem["approve_amount"].Text) < Convert.ToDouble(dataItem["total_amount"].Text))
        //        {
        //            dataItem["total_amount"].Style["font-weight"] = "bold";
        //            dataItem["approve_amount"].Style["font-weight"] = "bold";
        //            //dataItem["bgm_prj_name"].Style["font-weight"] = "bold";
        //            dataItem["approve_amount"].Style["color"] = "Red";
        //            //dataItem["bgm_prj_name"].Style["color"] = "Red";
        //        }
        //        else if (Convert.ToDouble(dataItem["approve_amount"].Text) > Convert.ToDouble(dataItem["total_amount"].Text))
        //        {
        //            dataItem["total_amount"].Style["font-weight"] = "bold";
        //            dataItem["approve_amount"].Style["font-weight"] = "bold";
        //            //dataItem["bgm_prj_name"].Style["font-weight"] = "bold";
        //            dataItem["approve_amount"].Style["color"] = "Blue";
        //            //dataItem["bgm_prj_name"].Style["color"] = "Blue";
        //        }

        //        Control target = e.Item.FindControl("hlChangePrj");
        //        if (!Object.Equals(target, null))
        //        {
        //            if (!Object.Equals(this.RadToolTipManager1, null))
        //            {
        //                this.RadToolTipManager1.TargetControls.Add(target.ClientID, "P-"+(e.Item as GridDataItem).GetDataKeyValue("Sequence").ToString(), true);
        //            }
        //        }
        //    }
        //    //if (e.Item is GridFooterItem)
        //    //{
        //    //    GridFooterItem footer = (GridFooterItem)e.Item;
        //    //    footer["bgm_prj_name"].Controls.Add(new LiteralControl("<span style='color: Black; font-weight: bold;'>รวมทั้งหมด :</span> "));
        //    //}
        //}

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "refresh")
            {
                LoadData();
            }
        }

        string tmpAmount;
        //protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        //{
        //    btnSaveSour.Visible = false;
        //    btnSavePrj.Visible = false;
        //    pnChange.Visible = true;
        //    lblChangeSeq.Text = "";
        //    tmpAmount = txtChange.Text;
        //    txtChange.Text = "";
        //    this.UpdateToolTip(args.Value, args.UpdatePanel);
        //}
        //private void UpdateToolTip(string elementID, UpdatePanel panel)
        //{
        //    panel.ContentTemplateContainer.Controls.Clear();
        //    panel.ContentTemplateContainer.Controls.Add(pnChange);
        //    string[] arg = elementID.Split('-');
        //    bool s = arg[0].ToString() == "S";
        //    lblChangeSeq.Text = arg[1].ToString();
        //    btnSaveSour.Visible = s;
        //    btnSavePrj.Visible = !s;
        //}

        //protected void btnSaveSour_Click(object sender, EventArgs e)
        //{
        //    decimal amount = tmpAmount.Length > 0 ? Convert.ToDecimal(tmpAmount) : 0;
        //    pjHeaderBLL.UpdateApprovalStrategy(lblChangeSeq.Text, amount, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
        //    RadAjaxManager1.ResponseScripts.Add("HideTooltipCurrent();"); 
        //    LoadDataSource();
        //    //txtChange.Text = "";
        //}
        //protected void btnSavePrj_Click(object sender, EventArgs e)
        //{
        //    decimal amount = tmpAmount.Length > 0 ? Convert.ToDecimal(tmpAmount) : 0;
        //    pjHeaderBLL.UpdateApprovalSource(lblChangeSeq.Text, amount, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
        //    RadAjaxManager1.ResponseScripts.Add("HideTooltipCurrent();");
        //    LoadDataProject();
        //    //txtChange.Text = "";
        //}
    }
}
